package net.trombonium.steroid;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class RecordMe {

	Button record;
	Button stop;
	Button play;
	MediaRecorder recorder = new MediaRecorder();
	String lastFile = "";
	
	public RecordMe(){}
	
	public String getLastFile(){
		return lastFile.substring(lastFile.lastIndexOf('/')+1);
	}

	public void record(int surveyId){
		String filename = "/sdcard/steroid/";
		filename += String.valueOf(surveyId);
		filename += "/results/";
		(new File(filename)).mkdir();
		filename += "recording";
		filename += String.valueOf(System.currentTimeMillis()) + ".3gp";
		filename += "";
		lastFile = filename;

		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		//recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //OutputFormat is MPEG
		recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
		//recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		recorder.setOutputFile(filename);

		recorder.prepare();
		recorder.start();
	}

	public void stop(){
		recorder.stop();
		recorder.release();
	}

	public void play() throws IllegalArgumentException, IllegalStateException, IOException{
		if(lastFile.length()>0){
			MediaPlayer mp = new MediaPlayer();
			mp.setDataSource(lastFile);  //steroid
			mp.prepare();
			mp.start();
		}
	} 	
}

